package com.jiuyin.dianjing.ui.activity.login;

import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.google.android.material.tabs.TabLayout;
import com.hyb.library.PreventKeyboardBlockUtil;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.fragment.ui.LoginFragment;
import com.jiuyin.dianjing.view.WrapContentHeightViewPager;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;

import static com.jiuyin.dianjing.constant.AppConstant.URL_FW;
import static com.jiuyin.dianjing.constant.AppConstant.URL_YS;
import static com.jiuyin.dianjing.ui.fragment.ui.LoginFragment.KEY_BUSINESS_TYPE;
import static com.jiuyin.dianjing.ui.fragment.ui.LoginFragment.TYPE_FORGOT_PWD;
import static com.jiuyin.dianjing.ui.fragment.ui.LoginFragment.TYPE_REGISTER;

public class LoginActivity extends BaseActivity {
    public static final String SHOW_PWD_LOGIN = "show_pwd_login";
    @BindView(R.id.layout_tabs)
    TabLayout layoutTabs;
    @BindView(R.id.view_pager)
    WrapContentHeightViewPager pager;
    @BindView(R.id.tv_protocol)
    TextView tvProtocol;
    @BindView(R.id.btn_login)
    Button btLogin;
    @BindView(R.id.tv_forget_pwd)
    View tvForgotPwd;
    @BindView(R.id.tv_register)
    View tvRegister;
    LoginFragment[] fragments = new LoginFragment[2];
    private String[] myTitle;
    private FragmentPagerAdapter pagerAdapter;
    private boolean showPwdLogin;

    @Override
    protected void beforeSetContentView() {
        View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        getWindow().setNavigationBarColor(Color.TRANSPARENT);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        myTitle = getResources().getStringArray(R.array.login_type);
        showPwdLogin = getIntent().getBooleanExtra(SHOW_PWD_LOGIN, true);
    }

    @Override
    protected void initView() {
        fragments[0] = LoginFragment.newInstance(LoginFragment.TYPE_LOGIN_WITH_PWD);
        fragments[1] = LoginFragment.newInstance(LoginFragment.TYPE_LOGIN_WITH_IDENTIFY_CODE);
        pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return myTitle.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return myTitle[position];
            }
        };
        pager.setAdapter(pagerAdapter);
        layoutTabs.setupWithViewPager(pager);
        if (!showPwdLogin) {
            pager.setCurrentItem(1);
        } else {
            pager.setCurrentItem(0);
        }
        pager.setOffscreenPageLimit(fragments.length);
        SpannableStringBuilder builder = new SpannableStringBuilder(getString(R.string.protocol));
        URLSpan ab = new URLSpan(URL_FW);
        builder.setSpan(ab, 8, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ab = new URLSpan(URL_YS);
        builder.setSpan(ab, 17, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvProtocol.setMovementMethod(new LinkMovementMethod());
        tvProtocol.setText(builder);

        btLogin.setOnClickListener(v -> {
            LoginFragment fragment = fragments[pager.getCurrentItem()];
            fragment.submit();
        });
        tvForgotPwd.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            intent.putExtra(KEY_BUSINESS_TYPE, TYPE_FORGOT_PWD);
            startActivity(intent);
        });
        tvRegister.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            intent.putExtra(KEY_BUSINESS_TYPE, TYPE_REGISTER);
            startActivity(intent);
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        PreventKeyboardBlockUtil.getInstance(this).setBtnView(btLogin).register();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        PreventKeyboardBlockUtil.getInstance(this).unRegister();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
